package com.atomscat.lottery.service;


import com.atomscat.lottery.entity.db.LotteryPrizeEntity;
import com.atomscat.lottery.entity.vo.request.LotteryPrizeRequest;
import com.atomscat.lottery.entity.vo.request.PageLotteryPrizeRequest;
import com.atomscat.lottery.entity.vo.request.UpdateLotteryPrizeRequest;
import com.atomscat.lottery.repository.LotteryActivityRepository;
import com.atomscat.lottery.repository.LotteryPrizeRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class LotteryPrizeService {

    private final LotteryPrizeRepository lotteryPrizeRepository;
    private final LotteryActivityRepository lotteryActivityRepository;

    /**
     * Creates a new lottery prize.
     *
     * @param lotteryPrizeRequest the request object containing details of the lottery prize to be created
     * @return true if the lottery prize is successfully created, false otherwise
     */
    public Boolean createLotteryPrize(LotteryPrizeRequest lotteryPrizeRequest) {
        LotteryPrizeEntity lotteryPrizeEntity = new LotteryPrizeEntity();
        lotteryActivityRepository.findById(lotteryPrizeRequest.getActivityId()).ifPresent(lotteryActivity -> {
            lotteryPrizeEntity.setPrizeName(lotteryPrizeRequest.getPrizeName());
            lotteryPrizeEntity.setPrizeQuantity(lotteryPrizeRequest.getPrizeQuantity());
            lotteryPrizeEntity.setPrizeType(lotteryPrizeRequest.getPrizeType());
            lotteryPrizeEntity.setLotteryActivity(lotteryActivity);
            lotteryPrizeEntity.setDescription(lotteryPrizeRequest.getDescription());
            lotteryPrizeRepository.save(lotteryPrizeEntity);
        });
        return lotteryPrizeEntity.getId() != null;
    }

    /**
     * Updates an existing lottery prize.
     *
     * @param lotteryPrizeRequest the request object containing updated details of the lottery prize
     * @return true if the lottery prize is successfully updated, false otherwise
     */
    public Boolean updateLotteryPrize(UpdateLotteryPrizeRequest lotteryPrizeRequest) {
        lotteryPrizeRepository.findById(lotteryPrizeRequest.getId()).map(lotteryPrizeEntity -> {
            lotteryPrizeEntity.setPrizeName(lotteryPrizeRequest.getPrizeName());
            lotteryPrizeEntity.setPrizeQuantity(lotteryPrizeRequest.getPrizeQuantity());
            lotteryPrizeEntity.setPrizeType(lotteryPrizeRequest.getPrizeType());
            lotteryPrizeEntity.setDescription(lotteryPrizeRequest.getDescription());
            return lotteryPrizeRepository.saveAndFlush(lotteryPrizeEntity);
        });
        return true;
    }

    /**
     * Retrieves a paginated list of lottery prizes based on the provided request parameters.
     *
     * @param pageLotteryPrizeRequest the request object containing pagination and filter parameters
     * @return a paginated list of lottery prizes
     */
    public Page<LotteryPrizeEntity> pageLotteryPrize(PageLotteryPrizeRequest pageLotteryPrizeRequest) {
        LotteryPrizeEntity lotteryPrizeEntity = new LotteryPrizeEntity();
        lotteryPrizeEntity.setPrizeName(pageLotteryPrizeRequest.getPrizeName());
        if (pageLotteryPrizeRequest.getActivityId() != null) {
            lotteryActivityRepository.findById(pageLotteryPrizeRequest.getActivityId()).ifPresent(lotteryPrizeEntity::setLotteryActivity);
        }
        PageRequest pageable = PageRequest.of(pageLotteryPrizeRequest.getPageNumber(), pageLotteryPrizeRequest.getPageSize());
        return lotteryPrizeRepository.findAll(Example.of(lotteryPrizeEntity), pageable);
    }
}
